﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PNProject.Models;
using System.Globalization;
using System.Web.Security;
using PNProject.EF;

namespace PNProject.Controllers
{
    
    public class MemberController : Controller
    {
        //
        // GET: /Member/

        public ActionResult Index()
        {
            var user = Membership.GetUser();
            if(user==null)
                return RedirectToAction("Login", "Member");
            else
                return RedirectToAction("Profile", "Member");
        }

        public ActionResult Login()
        {
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            return View(new LogOnModel());
        }

        [HttpPost]
        public ActionResult Login(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                bool flag = AdminService.CheckUser(model.UserName);
                if (flag && Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Profile", "Member");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            return View(model);
        }

        [Authorize]
        public ActionResult Profile()
        {
            var name = Membership.GetUser().UserName;
            var user = StaticData.xuserRepo.GetOne(a => a.Username == name);
            return View(user);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Profile(XUser model, string cpass, string npass, string rpass)
        {
            var user = StaticData.xuserRepo.GetOne(a => a.Username == model.Username);
            model.IsBlocked = user.IsBlocked;
            model.Role = user.Role;
            StaticData.xuserRepo.Save(user, model);
            if (!string.IsNullOrEmpty(cpass) && !string.IsNullOrEmpty(npass) && !string.IsNullOrEmpty(rpass))
            {
                bool changePasswordSucceeded = false;
                if (npass == rpass)
                {
                    MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true);
                    changePasswordSucceeded = currentUser.ChangePassword(cpass, npass);
                }
                if (changePasswordSucceeded)
                    Session["cp"] = "ok";
                else
                    Session["cp"] = "nok";
            }
            else
            {
                Session["cp"] = "0";
            }
            return RedirectToAction("Profile");
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            return View();
        }

    }
}
